// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// e [author] rhyme_lph
// l [email]  rhymelph@gmail.com
// E [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/account_entity.dart';
import 'package:finance_app/src/entities/reconciliation_item_entity.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/widgets/list_item/list_header_item.dart';
import 'package:finance_app/src/widgets/list_item/normal_list_item.dart';
import 'package:finance_app/src/widgets/pop/normal_border_pop.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/all_reports/bank_reconciliation/views/view_report_provider.dart';
import 'package:finance_app/src/provider/all_reports/bank_reconciliation/bank_reconciliation_provider.dart';

class ViewReportView extends StatefulWidget {
  @override
  _ViewReportViewState createState() => _ViewReportViewState();
}

class _ViewReportViewState extends State<ViewReportView>
    with
        StatefulPresenter<ViewReportView, BankReconciliationProvider,
            ViewReportProvider> {
  @override
  Widget buildWidget(BuildContext context) => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            AppSpaces.h20,
            listBuilder<AccountEntity>(
              selector: (_, __) => __.currentAccount,
              builder:
                  (BuildContext context, AccountEntity value, Widget child) =>
                      NormalBorderPop<AccountEntity>(
                select: value,
                buildDisplayName: (e) => e.bankName,
                hint: S.current.choose_account,
                onSelect: provider.onSelectAccount,
                group: provider.data,
              ),
            ),
            AppSpaces.h15,
            listBuilder<bool>(
                selector: (_, __) => __.currentAccount != null,
                builder: (BuildContext context, bool value, Widget child) =>
                    Visibility(
                      child: child,
                      visible: value,
                    ),
                child: Column(
                  children: [
                    DefaultTextStyle(
                      style: TextStyle(
                        color: aceBlackColor,
                        fontSize: getSp(16),
                        fontWeight: FontWeight.w700,
                        height: 22 / 16,
                      ),
                      child: Padding(
                        padding: EdgeInsets.only(
                          left: getDp(35),
                          right: getDp(40),
                          bottom: getDp(20),
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(S.current.closing_date),
                            Text(S.current.statement_balance),
                          ],
                        ),
                      ),
                    ),
                    ListHeaderItem(
                      title: S.current.reports,
                      padding: EdgeInsets.symmetric(
                        horizontal: getDp(35),
                      ),
                    ),
                  ],
                )),
            listBuilder<int>(
              selector: (_, __) => __.reconciliationList?.length ?? 0,
              builder: (BuildContext context, int value, Widget child) =>
                  ListView.builder(
                physics: NeverScrollableScrollPhysics(),
                itemBuilder: _buildItems,
                itemCount: value,
                shrinkWrap: true,
              ),
            ),
          ],
        ),
      );

  Widget _buildItems(BuildContext context, int index) {
    return listBuilder<ReconciliationItemEntity>(
      selector: (_, __) => __.reconciliationList[index],
      builder: (BuildContext context, ReconciliationItemEntity value,
              Widget child) =>
          NormalListItem(
        title: DateTimeUtils.formatDateTimeString(value.endDate),
        onTap: () {
          provider.onNavViewReport(value);
        },
        right:
            '${provider.currentAccount.currencyCode} ${value.statementBalance}',
      ),
    );
  }
}
